/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.colswe.testingProject.control;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
import org.colswe.testingProject.entity.Person;
import org.colswe.testingProject.view.EditUserPanel;
import org.colswe.testingProject.view.LoginWindow;

/**
 *
 * @author Leonardo Bohórquez
 */
public class JTableButtonRenderer implements TableCellRenderer {

    private EditUserPanel pnlEdit;
    private PersonControl pc;

    @Override
    public Component getTableCellRendererComponent(final JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        JButton cmdButton = (JButton) value;
        if (isSelected) {
            cmdButton.setForeground(table.getSelectionForeground());
            cmdButton.setBackground(table.getSelectionBackground());
        } else {
            cmdButton.setForeground(table.getForeground());
            cmdButton.setBackground(table.getBackground());
        }
        if (cmdButton.getText().equals("Eliminar")) {
            cmdButton.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent arg0) {
                    int answer = JOptionPane.showConfirmDialog(null, "¿Realmente desea eliminar este usuario?", "Confirmar eliminación", JOptionPane.YES_NO_OPTION);
                    if (answer == 0) {
                        pc = new PersonControl();
                        boolean flag = pc.delete(new Person(table.getModel().getValueAt(table.getSelectedRow(), 0).toString(), null, null));
                        if (flag) {
                            JOptionPane.showMessageDialog(null, "Se ha eliminado el usuario", "Borrado confirmado", JOptionPane.INFORMATION_MESSAGE);
                        }else{
                            JOptionPane.showMessageDialog(null, "No se pudo borrar el usuario", "Error al borrar", JOptionPane.ERROR_MESSAGE);
                        }
                    }
                }
            });
        } else {
            cmdButton.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent arg0) {
                    pnlEdit = new EditUserPanel(false);
                    LoginWindow.mf.tbpnlTabs.add(pnlEdit);
                    LoginWindow.mf.tbpnlTabs.setSelectedIndex(
                            LoginWindow.mf.tbpnlTabs.getTabCount() - 1);
                    LoginWindow.mf.tbpnlTabs.setTitleAt(
                            LoginWindow.mf.tbpnlTabs.getTabCount() - 1,
                            "Editar usuario");
                    LoginWindow.mf.tbpnlTabs.setEnabled(false);
                }
            });
        }
        return cmdButton;
    }
}
